﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Core
{
    public class UIEventListener : EventTrigger
    {
        public event Action<GameObject> OnClick;
        public event Action<GameObject> OnDown;
        public event Action<GameObject> OnEnter;
        public event Action<GameObject> OnExit;
        public event Action<GameObject> OnUp;
        public event Action<GameObject, PointerEventData> OnBeginDragEvent;
        public event Action<GameObject, PointerEventData> OnDragEvent;
        public event Action<GameObject, PointerEventData> OnDropEvent;

        public static UIEventListener Get(GameObject go)
        {
            return go.GetComponent<UIEventListener>() ??
                   go.AddComponent<UIEventListener>();
        }

        public static UIEventListener Get(UIWidget widget)
        {
            return Get(widget.gameObject);
        }

        public override void OnPointerClick(PointerEventData eventData)
        {
            //Debug.Log("Click");
            OnClick?.Invoke(gameObject);
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            //Debug.Log("Down");
            OnDown?.Invoke(gameObject);
        }

        public override void OnPointerEnter(PointerEventData eventData)
        {
            //Debug.Log("Enter");
            OnEnter?.Invoke(gameObject);
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            //Debug.Log("Exit");
            OnExit?.Invoke(gameObject);
        }

        public override void OnPointerUp(PointerEventData eventData)
        {
            //Debug.Log("Up");
            OnUp?.Invoke(gameObject);
        }

        public override void OnBeginDrag(PointerEventData eventData)
        {
            //base.OnBeginDrag(eventData);
            OnBeginDragEvent?.Invoke(gameObject, eventData);
        }

        public override void OnDrag(PointerEventData eventData)
        {
            //Debug.Log("Drag");
            OnDragEvent?.Invoke(gameObject, eventData);
        }

        public override void OnDrop(PointerEventData eventData)
        {
            //Debug.Log("Drop");
            OnDropEvent?.Invoke(gameObject, eventData);
        }

        public void AddListener(EUIEventType eventType, Action<GameObject> callback)
        {
            switch (eventType)
            {
                case EUIEventType.Click: OnClick += callback; break;
                case EUIEventType.Down: OnDown += callback; break;
                case EUIEventType.Up: OnUp += callback; break;
                default: break;
            }
        }

        public void RemoveListener(EUIEventType eventType, Action<GameObject> callback)
        {
            switch (eventType)
            {
                case EUIEventType.Click: OnClick -= callback; break;
                case EUIEventType.Down: OnDown -= callback; break;
                case EUIEventType.Up: OnUp -= callback; break;
                default: break;
            }
        }
    }
}
